﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using Gonte.Wpf.Controls.ViewModel;

using MyWpfControlsTestBench.Model;
using MyWpfControlsTestBench.ViewModel;

namespace MyWpfControlsTestBench.Demos
{
    /// <summary>
    /// Interaction logic for TreeViewDemoWindow.xaml
    /// </summary>
    public partial class TreeViewDemoWindow 
        : Window
    {
        public TreeViewDemoWindow()
        {
            InitializeComponent();

            RootTreeItem root = new RootTreeItem();
            root.Name = "Root";

            for (int i = 0; i < 10; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child " + (i + 1).ToString();

                root.Children.Add(child);
            }

            viewModel_ = new RootTreeItemViewModel();

            for (int i = 10; i < 20; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child " + (i + 1).ToString();

                ChildTreeItemViewModel childViewModel = new ChildTreeItemViewModel(child, viewModel_);
                viewModel_.AddChild(childViewModel);
            }

            viewModel_.Label = "Empty Root";

            viewModel_ = new RootTreeItemViewModel();

            for (int i = 0; i < 20; ++i)
            {
                ChildTreeItem childItem = new ChildTreeItem();
                childItem.Name = "Item " + (i + 1).ToString();

                ChildTreeItemViewModel childModel = new ChildTreeItemViewModel(childItem, viewModel_);
                viewModel_.AddChild(childModel);
            }

            DataContext = viewModel_;
        }

        #region Event Handlers

        /// <summary>
        /// Selects the item when it is right clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTreeViewItemMouseRightButtonDown(object sender, MouseEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            if (item != null)
            {
                item.Focus();
                e.Handled = true;
            }
        }

        /// <summary>
        /// Updates the property grid with the selected tree item value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTreeViewSelectedItemChanged(Object sender, RoutedPropertyChangedEventArgs<Object> e)
        {
            TreeView selected = (TreeView)e.OriginalSource;
            ViewModelBase viewModel = selected.SelectedValue as ViewModelBase;

            if (null != viewModel.SelectionChanged)
            {
                viewModel.SelectionChanged(sender, new ViewModelEventArgs(viewModel));
            }

            ContextMenu = viewModel.CreateContextMenu();
        }

        #endregion

        #region Fields

        private RootTreeItemViewModel viewModel_;

        #endregion
    }
}
